package com.zhiche.lisa.bms.controller.charge;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.charge.ChargeOffRule;
import com.zhiche.lisa.bms.dao.model.quote.Quote;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.service.charge.ChargeOffRuleService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出账规则 前端控制器
 * </p>
 *
 * @author liuanshun
 * @since 2018-11-02
 */
@RestController
@RequestMapping("/chargeOffRule")
@Api(value = "/chargeOffRule", description = "出账规则接口", tags = {"出账规则接口"})
public class ChargeOffRuleController {
    private final Logger LOGGER = LoggerFactory.getLogger(ChargeOffRuleController.class);

    @Autowired
    private ChargeOffRuleService chargeOffRuleService;

    @PostMapping(value = "/getChargeOffRulePage")
    @ApiOperation(value = "根据条件查询出账规则", notes = "根据条件查询出账规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<ChargeOffRule>> getChargeOffRulePage(@RequestBody Page<ChargeOffRule> page) {
        LOGGER.info("/ChargeOffRuleController.getChargeOffRulePage Param {} ", page);
        RestfulResponse<Page<ChargeOffRule>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<ChargeOffRule> quotePage = chargeOffRuleService.selectChargeOffRulePage(page);
            restful.setData(quotePage);
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.getChargeOffRulePage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.getChargeOffRulePage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/getChargeOffRuleList")
    @ApiOperation(value = "根据条件查询出账规则", notes = "根据条件查询出账规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<ChargeOffRule>> getChargeOffRuleList(@RequestBody Map<String, Object> condition) {
        LOGGER.info("/ChargeOffRuleController.getChargeOffRuleList Param {} ", condition);
        RestfulResponse<List<ChargeOffRule>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<ChargeOffRule> quotePage = chargeOffRuleService.selectChargeOffRuleList(condition);
            restful.setData(quotePage);
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.getChargeOffRuleList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.getChargeOffRuleList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/addChargeOffRule")
    @ApiOperation(value = "新增出账规则", notes = "新增出账规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<ChargeOffRule> addChargeOffRule(@RequestBody ChargeOffRule chargeOffRule) {
        LOGGER.info("/ChargeOffRuleController.addChargeOffRule Param {} ", chargeOffRule);
        RestfulResponse<ChargeOffRule> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            ChargeOffRule quotePage = chargeOffRuleService.insertChargeOffRule(chargeOffRule);
            restful.setData(quotePage);
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.addChargeOffRule ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.addChargeOffRule ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/selectOffRuleCheckRecAgingByFleetId/{fleetId}")
    @ApiOperation(value = "查询以检查对账信息的出账规则", notes = "查询以检查对账信息的出账规则", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<ChargeOffRule> selectOffRuleCheckRecAgingByFleetId(@PathVariable("fleetId") Long fleetId) {
        LOGGER.info("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId Param {} ", fleetId);
        RestfulResponse<ChargeOffRule> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Calendar cal = Calendar.getInstance();
            int day = cal.get(Calendar.DATE);
            ChargeOffRule chargeOffRule = chargeOffRuleService.selectChargeOffRuleByFleetId(fleetId, null);
            restful.setData(chargeOffRule);
            if (chargeOffRule != null) {
                int recAging = chargeOffRule.getRecAging() == 0 ? 6 : (chargeOffRule.getRecAging() == 1 ? 15 : 20);
                if (day != recAging) {
                    restful.setCode(-1);
                }
                restful.setMessage("不在对账期限内");
            }
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/checkOffRulePayDaysByFleetId")
    @ApiOperation(value = "查询以检查对账信息的出账规则", notes = "查询以检查对账信息的出账规则", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<ChargeOffRule> checkOffRulePayDaysByFleetId(@RequestParam("fleetId") Long fleetId, @RequestParam("type") String type) {
        LOGGER.info("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId Param {} ", fleetId);
        RestfulResponse<ChargeOffRule> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            ChargeOffRule chargeOffRule = chargeOffRuleService.checkPayDays(fleetId, type);
            restful.setData(chargeOffRule);
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.selectOffRuleCheckRecAgingByFleetId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/updateChargeOffRule")
    @ApiOperation(value = "修改出账规则", notes = "修改出账规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Quote.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<ChargeOffRule> updateChargeOffRule(@RequestBody ChargeOffRule chargeOffRule) {
        LOGGER.info("/ChargeOffRuleController.getChargeOffRuleList Param {} ", chargeOffRule);
        RestfulResponse<ChargeOffRule> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            ChargeOffRule quotePage = chargeOffRuleService.updateChargeOffRule(chargeOffRule);
            restful.setData(quotePage);
        } catch (BaseException e) {
            LOGGER.error("/ChargeOffRuleController.updateChargeOffRule ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/ChargeOffRuleController.updateChargeOffRule ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/open")
    @ApiOperation(value = "修改启用状态为启用", notes = "修改启用状态为启用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> open(@RequestBody CommonVO<Quote> commonVO) {
        LOGGER.info("/QuoteController.open Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            for (Long id :
                    commonVO.getIds()) {
                try {
                    chargeOffRuleService.openChargeOffRule(id);
                    msg.add("启用请求::成功");
                } catch (BaseException e) {
                    msg.add("启用请求::" + e.getMessage());
                    restful.setCode(-2);
                } catch (Exception e) {
                    msg.add("启用请求::启用失败");
                    restful.setCode(-2);
                }
            }
            restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("/QuoteController.open ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/QuoteController.open ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/close")
    @ApiOperation(value = "修改启用状态为禁用", notes = "修改启用状态为禁用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> close(@RequestBody CommonVO<Quote> commonVO) {
        LOGGER.info("/QuoteController.close Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            for (Long id :
                    commonVO.getIds()) {
                try {
                    chargeOffRuleService.closeChargeOffRule(id);
                    msg.add("禁用请求::成功");
                } catch (BaseException e) {
                    msg.add("禁用请求::" + e.getMessage());
                    restful.setCode(-2);
                } catch (Exception e) {
                    msg.add("禁用请求::禁用失败");
                    restful.setCode(-2);
                }
            }
            restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("/QuoteController.close ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/QuoteController.close ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/bindPayable")
    @ApiOperation(value = "绑定出账", notes = "绑定出账", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> bindPayable(@RequestBody CommonVO<Quote> commonVO) {
        LOGGER.info("/QuoteController.bindPayable Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            for (Long id : commonVO.getIds()) {
                try {
                    chargeOffRuleService.payable(id, null, null);
                    msg.add("出账请求::成功");
                } catch (BaseException e) {
                    msg.add("出账请求::" + e.getMessage());
                    restful.setCode(-2);
                } catch (Exception e) {
                    msg.add("出账请求::出账失败");
                    restful.setCode(-2);
                }
            }
            restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("/QuoteController.bindPayable ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/QuoteController.bindPayable ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }
}

